package com.chuanke.ckfamily.service.user.thread;

import com.chuanke.ckfamily.dao.user.model.User;
import com.chuanke.ckfamily.service.social.thread.LoadFansToFamilyRunable;
import com.chuanke.ckfamily.service.user.UserService;
import com.chuanke.ckfamily.service.util.CkServerUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import java.util.List;

/**
 * 转移用户粉丝数据线程
 * @author xiaoxia
 *
 */
public class LoadFansRunnable implements Runnable {

	private static final Logger log = LoggerFactory.getLogger(LoadFansToFamilyRunable.class);

	private Long uid;
	
    private UserService userService;
    
    public LoadFansRunnable(UserService userService, Long uid){
    	this.uid = uid;
    	this.userService = userService;
    }
    
	@Override
	public void run() {
    	log.info("begin load fans");
    	boolean isEnd = false;
    	int pageIndex = 0;
		while(!isEnd){
			try {
				List<User> fansList = CkServerUtil.loadFans(uid, pageIndex);
				log.info("load fans for=============> fans={}", fansList.size());
				if (fansList == null || fansList.isEmpty()) {
					log.info("load fans for uid={}, page={}", uid, pageIndex);
					break;
				}

				log.info("load fans for uid={}, page={}, fanslist={}", uid, pageIndex, fansList.size());
				for (User user: fansList) {
					user.setSuper_id(uid);
					userService.save(user);
				}

				isEnd = fansList.size() < 50;
				pageIndex++;
				//txManager.commit(status);

			} catch (Exception e) {
				e.printStackTrace();
				isEnd = true;
				// 回滚
				//txManager.rollback(status);

			} finally {
				// 提交

			}
		}
	}
}
